package com.shiwaixiangcun.customer.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.mobstat.SendStrategyEnum;
import com.baidu.mobstat.StatService;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.shiwaixiangcun.customer.ConfigSps;
import com.shiwaixiangcun.customer.GlobalApi;
import com.shiwaixiangcun.customer.R;
import com.shiwaixiangcun.customer.base.BaseActivity;
import com.shiwaixiangcun.customer.entity.FeedbackBean;
import com.shiwaixiangcun.customer.entity.ResponseEntity;
import com.shiwaixiangcun.customer.network.NetworkHelper;
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr;
import com.shiwaixiangcun.customer.utils.JsonUtil;
import com.shiwaixiangcun.customer.utils.LoginOutUtil;
import com.shiwaixiangcun.customer.utils.RefreshTokenUtil;
import com.shiwaixiangcun.customer.widget.ChangeLightImageView;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;

/**
 * 意见反馈
 *
 * @author Administrator
 */
public class FeedBackActivity extends BaseActivity implements View.OnClickListener {

    private ChangeLightImageView back_left;
    private TextView tv_page_name;
    private TextView tv_top_right;
    private EditText post_content;
    private NetworkHelper mNetworkHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        //        百度统计
        StatService.setLogSenderDelayed(10);
        StatService.setSendLogStrategy(this, SendStrategyEnum.APP_START, 1, false);
        StatService.setSessionTimeOut(30);
        mNetworkHelper = new NetworkHelper(this, getSupportFragmentManager());
        layoutView();
        initData();
    }

    private void layoutView() {
        back_left = findViewById(R.id.back_left);
        tv_page_name = findViewById(R.id.tv_page_name);
        tv_top_right = findViewById(R.id.tv_top_right);
        post_content = findViewById(R.id.post_content);
    }

    private void initData() {

        tv_page_name.setText("意见反馈");
        tv_top_right.setVisibility(View.VISIBLE);
        tv_top_right.setText("提交");
        tv_top_right.setTextColor(Color.parseColor("#1CCC8C"));
        back_left.setOnClickListener(this);
        tv_top_right.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.back_left:
                finish();
                break;
            case R.id.tv_top_right:
                feedBack(post_content.getText().toString().trim());

                break;
            default:
                break;
        }
    }

    private void feedBack(String trim) {
        if (trim.isEmpty()) {
            showToastShort("请输入反馈内容");

        } else {
            HttpParams httpParams = new HttpParams();
            httpParams.put("content", trim);
            httpParams.put("dataSource", "PROPERTY_CUSTOMER_APP");
            final String strRefreshToken = (String) AppSharePreferenceMgr.get(mContext, ConfigSps.CURRENT_REFRESH_TOKEN, "");

            mNetworkHelper.postRequestWithToken(GlobalApi.INSTANCE.getFeedBack(),
                    true, httpParams, new NetworkHelper.NetWorkHelperBack() {
                        @Override
                        public void requestSuccess(@NotNull String json) {
                            Type type = new TypeToken<ResponseEntity<FeedbackBean>>() {
                            }.getType();
                            ResponseEntity<FeedbackBean> responseEntity = JsonUtil.fromJson(json, type);

                            assert responseEntity != null;
                            if (responseEntity.getResponseCode() == 1001) {
                                showToastShort("反馈成功");
                                finish();
                            } else if (responseEntity.getResponseCode() == 1018) {
                                RefreshTokenUtil.refreshToken(mContext, strRefreshToken);
                            } else if (responseEntity.getResponseCode() == 1019) {
                                LoginOutUtil.sendLoginOutUtil(mContext);
                            } else {
                                showToastShort(responseEntity.getMessage());
                            }
                        }

                        @Override
                        public void requestFail() {
                            showToastShort("网络异常，请稍后再试");

                        }
                    });


        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        StatService.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPause(this);
    }
}
